#ifndef __APP_H
#define __APP_H

#include "OS_CPU.H"
#include "Cpu.H"
#include "FT32F10x.h"
#include "Macro.h"
#include "TM1620.h"
#include "Position.h"
#include "Flash.h"

/************************************************************************************************
*				TASK STACK AND PRIORITY DEFINITION                                              *
************************************************************************************************/
#define MAX_SUPERJOB_STK_SIZE	  300//50
#define cPRIO_SUPERJOB		      0			   		

#define MAX_SCIJOB_STK_SIZE       300	
#define cPRIO_SCIJOB              2

#define MAX_GENEVENTJOB_STK_SIZE  300 //50		
#define cPRIO_GENEVENTJOB         1
                                  
                                 

/***********************************************************************************************
*				TASK EVENT DEFINITION                                                          *
***********************************************************************************************/

/*******************************************************************
//event definition																		*
*******************************************************************/
//sSuperTask task
#define eSuperTimer			        0
#define eSuperFault			        1
#define eSuperSaveFlash			    2
#define eSuperOnUps			        3
//#define eSuperDugUps			    4
#define eSuperLineLoss			    4
#define eSuperBatDisConnect		    5
#define eSuperAskBatTest		    6
#define eSuperECOLoss		        7

//sSciTask task
#define eSciTimer			        0
#define eSciTxCEOcmd			    1
#define eSciTxCEcmd		            2
#define eSciTxCFcmd		            3
#define eSciTxCAcmd		            4
#define eSciTxMode		            5
#define eSciTxCBcmd                 6


//GenEvent task
#define eGenEventTimer					0
#define eGenTask500ms              		1	// 
#define eGen40Ms	              		2	//




/**********************************************************************************************
	system time cnt timer tick = 1ms                                                          *
***********************************************************************************************/
#define 	cT4ms			4
#define 	cT10ms			10
#define 	cT20ms			20
#define 	cT30ms			30
#define 	cT50ms			50
#define 	cT100ms			100
#define 	cT500ms			500
#define 	cT300ms			300
#define 	cT400ms			400
#define 	cT1s			1000
#define 	cT4s			4000
/*******************************************************************************
*Constant declaration
*******************************************************************************/
#define		cFaultType		    1
#define		cWarning1Type		2
#define		cWarning2Type		3
#define		cBack				2
#define		cRunning			3
#define		cTrue				1
#define		cFalse				0


#define		cBtnMax				4	// 按键板最大数量

// CUP时间宏定义
#define cCupTimeLmt				(60*60)	// 1 hour
#define cCupTimeDefault			20		// 

// 消毒时间宏定义
#define cDisinfTimeDefault		5		// 

// 冲水时间宏定义
#define cWaterTimeCase1			30
#define cWaterTimeCase2			(3*60)
#define cWaterTimeCase3			(15*60)
#define cWaterTimeCase4			0xFFFFFFFF

#define cWaterTimeDefault		cWaterTimeCase1		// 

#define cSeatI2C		I2C2
#define cStBkI2C		I2C1


enum COMDEVICE
{
	ComMainBrd = 0,		// 主控(程控器)			USART3
	ComDevice1 = 1,		// 设备1				USART2
	ComDevice2 = 2,		// 设备2				UART4
	ComDevice3 = 3		// 设备3				UART5
};

enum SETSTATUS
{
	cSetIdle		= 0,	// 设置空闲    
	cSetPend,               // 设置待定    
	cSetP1Pst,              // P1位置设置  
	cSetP2Pst,              // P2位置设置  
	cSetP3Pst,              // P3位置设置  
	cSetCupTime,            // 供水时间设置  
	cSetWaterTime,          // 冲盂时间设置  
	cSetLmtPstManu,         // 极限位置手动设置
	cSetLmtPstAuto			// 极限位置自动设置
};

enum MOTOR_MODE
{
	cMotorNormal = 0,		// 常规   
   	cMotorResetMode, 		// 电机复位 
	cMotorPreSetMode,       // 电机预复位
	cMotorClearMode,		// 清洗
	cMotorSpiteMode,		// 吐痰
	cMotorP1Mode,           // P1模式 
	cMotorP2Mode,           // P2模式 
	cMotorP3Mode,           // P3模式 
	cMotorManuSetMode,      // 电机手动极限设置 
	cMotorAutoSetMode,      // 电机自动极限设置 
	cMotorLockMode,			// 机椅互锁
	cMotorEmgMode           // 遇阻   
};  

enum WATERRUNSTS
{
	cWaterRunInit		= 0,	// 未运行    
	cWaterRunDisinf,			// 消毒    
	cWaterRunWater,             // 冲水 
};


enum FAULTCODE
{
	cFaultNone			= 0,	//     
	cSeatComFault,				//     
	cStBkComFault,				// 
};

enum WATERSETSTS
{	
	cWaterSetInit		= 0,	// 未运行
	cWaterSetDisinf,			// 消毒    
	cWaterSetWash,       		// 冲水 
};

enum CDOCTORROLE
{	
	cDoctorA		= 0,
	cDoctorB,
	cDoctorC,
};

enum CEMG_STS
{	
	cEmgStop		= 0,
	cEmgUp,
};


enum CSPITE_STS
{	
	cSpiteInit		= 0,
	cSpiteRun,
	cSpiteBack
};



union FLASH_SAVE
{
	INT32U	dword;
	struct	
	{
		INT32U  SetCupTm:1;			// CUP时间被设置
		INT32U  SetDisinfTm:1;		// Water消毒时间设置
		INT32U  SetWashTm:1;		// Water冲洗时间设置
		INT32U  SetP1:1;			// P1位置设定

		INT32U  SetP2:1;			// P2位置设定
		INT32U  SetP3:1;			// P3位置设定
		INT32U  SetLmtPstManu:1;	// 手动极限位置设定
		INT32U  SetLmtPstAuto:1;	// 手动极限位置设定

		
		INT32U  Reserved:24;
	}Bits;
};





union SYSBIT_1
{
	INT32U	dword;
	struct	
	{
		INT32U  SciIniOK:1;			// SCI初始化完成标志
		INT32U  SetSaving:1;		// 设置保存中
		INT32U  PCtrBegin:1;		// 开始P控制		
		
		INT32U  PCtrSeatEnd:1;		// P座椅控制结束		
		INT32U  PCtrStBkEnd:1;		// P靠背控制结束

		INT32U  LmtSeatAutoUp:1;	// 
		INT32U  LmtStBkAutoUp:1;	// 
		INT32U  LmtSeatAutoEnd:1;	//		
		INT32U  LmtStBkAutoEnd:1;	//

		INT32U  SeatFault:1;
		INT32U  StBkFault:1;
		
		INT32U  Reserved:21;
	}Bits;
};

union SYSSTS1
{
	INT32U	dword;
	struct	
	{
		INT32U  P1Sts:1;			// P1键状态		
		INT32U  P2Sts:1;			// P2键状态
		INT32U  P3Sts:1;			// P3键状态
		INT32U  HeatBtnOn:1;		// 加热按键按下

		INT32U  ResetSts:1;			// 位置重置
		INT32U  PresetSts:1;		// 位置反重置
		INT32U  Up:1;				// 座椅上升
		INT32U  Down:1;				// 座椅下降

		INT32U  Bu:1;				// 靠背上升
		INT32U  Bn:1;				// 靠背下降
		
		INT32U	SeatSts:2;			// 座椅状态
		INT32U	StBkSts:2;			// 靠背状态
		
		INT32U	Cup:1;				// 供水
		INT32U	Water:2;			// 冲盂状态
		INT32U	LampOn:1;			// 口腔灯打开

		INT32U	WatchOn:1;			// 观片灯打开
		INT32U	DisinfOnly:1;		// 冲盂，仅消毒
		
		INT32U  Reserved:14;
	}Bits;
};

union LCLBTNSTS1		// 本地按键
{
	INT32U	Byte;
	struct	
	{
		INT32U  LmtPstSeatUp:1;		// 座椅上升限位
		INT32U	LmtPstSeatDown:1;	// 座椅下降限位
		INT32U	LmtPstStBkUp:1;		// 靠背后仰限位
		INT32U	LmtPstStBkDown:1;	// 靠背后仰限位

		INT32U	EmgSeatDn:1;		// 座椅下降遇阻,紧急操作
		INT32U	EmgSeatDnActive:1;	// 座椅下降遇阻,紧急操作,下降沿生效
		INT32U	EmgStbkDn:1;		// 靠背下降(后仰)遇阻,紧急操作
		INT32U	EmgStBkDnActive:1;	// 座椅下降遇阻,紧急操作,下降沿生效

		INT32U  MotorLock:1;		// 
		
		INT32U  Reserved:23;
	}Bits;
};

union FT_CTR_BTN		// 脚控
{
	INT8U	Byte;
	struct	
	{
		INT8U  BtnUp:1;
		INT8U  BtnDn:1;
		INT8U  BtnBu:1;
		INT8U  BtnBn:1;

		INT8U  BtnCup:1;
		INT8U  BtnWater:1;
 		INT8U  BtnSpite:1;
		INT8U  BtnDisinf:1;
	}Bits;
};



union LED_MATRIX1
{
	INT8U	Byte;
	struct	
	{
		INT8U  A:1;
		INT8U  B:1;
		INT8U  C:1;
		INT8U  Lp:1;
		
		INT8U  W1:1;
		INT8U  W2:1;
		INT8U  HEAT:1;
		INT8U  LAMP:1;
		
	}Bits;
};

union LED_MATRIX2
{
	INT8U	Byte;
	struct	
	{
		INT8U  SET:1;
		INT8U  Watch:1;

		INT8U  Reserverd:6;
	}Bits;
};




union BTN_MATRIX1
{
	INT8U	Byte;
	struct	
	{
		INT8U  BtnS_Up:1;
		INT8U  BtnPreset:1;
		INT8U  BtnReset:1;
		INT8U  BtnP1:1;

		INT8U  BtnDOC:1;
		INT8U  BtnCF:1;
		INT8U  BtnSet:1;
		INT8U  Reserved:1;
	}Bits;
};

union BTN_MATRIX2
{
	INT8U	Byte;
	struct	
	{
		INT8U  BtnS_DN:1;
		INT8U  BtnUp:1;
		INT8U  BtnBu:1;			// 靠背上升		
		INT8U  BtnP2:1;
				
		INT8U  BtnSpite:1;	
		INT8U  BtnHeat:1;			// 
		INT8U  BtnLight:1;		// 口腔灯
		INT8U  Reserved:1;		
	}Bits;
};

union BTN_MATRIX3
{
	INT8U	Byte;
	struct	
	{
		INT8U  BtnWatch:1;
		INT8U  BtnDn:1;		
		INT8U  BtnBn:1;			// 靠背下降
		INT8U  BtnP3:1;

		INT8U  Reserved1:1;
		INT8U  BtnCup:1;			// 
		INT8U  BtnWater:1;
		INT8U  Reserved2:1;
	}Bits;
};


typedef struct
{
    INT32U  P_Seat;	// 座椅位置
    INT32U  P_StBk;	// 靠背位置		
}MEM_PLACE_ANGLE;

struct  MEM_PLACE
{
    MEM_PLACE_ANGLE P1;
	MEM_PLACE_ANGLE P2;
	MEM_PLACE_ANGLE P3;
//	MEM_PLACE_ANGLE Curr;			// 当前位置
	MEM_PLACE_ANGLE UpLmt;			// 座椅上升/靠背前倾 极限位置
	MEM_PLACE_ANGLE DnLmt;			// 座椅下降/靠背后仰 极限位置
	INT32U CupTime;					// CUP定时时间
	INT32U DisinfTime;				// 消毒水定时间
	INT32U WashCase;				// 清水冲刷时间
};

struct DOCTOR
{
	INT32U Role;
	struct MEM_PLACE DocA;
	struct MEM_PLACE DocB;
	struct MEM_PLACE DocC;
};

extern struct  MEM_PLACE MemPlace;
extern MEM_PLACE_ANGLE CurrPst;
extern MEM_PLACE_ANGLE RecordPst;
extern struct DOCTOR Doctor;


union CTR_IO1
{
	INT8U	Byte;
	struct	
	{
		INT8U  WaterOn:1;			// 冲盂
		INT8U  CupOn:1;				// 水杯
		INT8U  DisinfOn:1;			// 消毒
		INT8U  S_Suction:1;			// 强吸
		
		INT8U  W_Suction:1;			// 弱吸		
		INT8U  InstTrayHeatOn:1;	// 器械盘加热
		INT8U  LampOn:1;			// 口腔灯
		INT8U  SideBoxHeatOn:1;		// 侧箱加热

	}Bits;
};

union CTR_IO2
{
	INT8U	Byte;
	struct	
	{
		INT8U  GdBoxSolValue1On:1;	// 地箱电磁阀1
		INT8U  GdBoxSolValue2On:1;	// 地箱电磁阀2
		
		INT8U  Reserved:6;
		
	}Bits;
};



struct MAINBRDDATA
{
	union LED_MATRIX1 CtrLed1;
	union LED_MATRIX2 CtrLed2;
	INT8U LedReserved1;					// LED控制保留
	INT8U LedReserved2;					// LED控制保留
	union CTR_IO1 CtrIo1;
	union CTR_IO2 CtrIo2;
	INT8U IOReserved1;					// IO控制保留
	INT8U IOReserved2;					// IO控制保留
};   



extern struct MAINBRDDATA MainBrdData;



extern union SYSBIT_1 SysBit_1;
extern union SYSSTS1 SysSts1;
extern union LCLBTNSTS1 LclBtnSts1;
extern union FLASH_SAVE FlashSave;
extern union FT_CTR_BTN FtCtrBtn;
extern union FT_CTR_BTN FtCtrBtnOld;


extern INT8U   ubFaultCode;
extern INT16U  uwTest;
extern INT16U  uwTest1;
extern INT16U  uwTest2;

extern union LED_MATRIX1  LedMatrix1[cBtnMax];
extern union LED_MATRIX2  LedMatrix2[cBtnMax];
extern union LED_MATRIX1  LedMatrix1Old[cBtnMax];
extern union LED_MATRIX2  LedMatrix2Old[cBtnMax];


extern union BTN_MATRIX1 BtnMatrix1[cBtnMax];
extern union BTN_MATRIX2 BtnMatrix2[cBtnMax];
extern union BTN_MATRIX3 BtnMatrix3[cBtnMax];
extern union BTN_MATRIX1 BtnMatrix1Old[cBtnMax];
extern union BTN_MATRIX2 BtnMatrix2Old[cBtnMax];
extern union BTN_MATRIX3 BtnMatrix3Old[cBtnMax];


// 蜂鸣器宏定义
#define		cBuzPrdS2			5 // 40*5
#define		cBuzCmpS2SecShort	3  //


#define		cBuzPrdS5			12 // 40*12
#define		cBuzCmpS5SecShort	5  //
#define		cBuzCmpS5SecLong	10 //
#define		cBuzCmpS5SecAll		12 //

#define		cBuzPrd1S			25 // 40*25
#define		cBuzCmp1SecShort	10 //
#define		cBuzCmp1SecLong		20 //
#define		cBuzCmp1SecAll		25 //

#define		BuzzerSet(num,prd,cmp)	uwBuzzerTimes = num;\
									uwBuzzerPeriod = prd;\
									uwBuzzerCmp = cmp;\
									uwBuzzerCnt = 0

#define		sBuzOnce()				BuzzerSet(1,cBuzPrdS5,cBuzCmpS5SecShort)
#define		sBuzTwice()				BuzzerSet(2,cBuzPrdS2,cBuzCmpS2SecShort)
#define		sBuzThird()				BuzzerSet(3,cBuzPrdS2,cBuzCmpS2SecShort)
#define		sBuzfour()				BuzzerSet(4,cBuzPrdS2,cBuzCmpS2SecShort)

#define		sBuzWarn()				BuzzerSet(3,cBuzPrdS2,cBuzCmpS2SecShort)


// LED周期设置
#define		mLedSet(num,prd,cmp)	uwLedTimes = num;\
									uwLedPeriod = prd;\
									uwLedCmp = cmp;\
									uwLedCnt = 0



#define		cWashSetCaseMax			4





extern INT16U uwBuzzerCmp;		// 鸣叫长短
extern INT16U uwBuzzerPeriod;	// 鸣叫周期
extern INT16U uwBuzzerTimes;	// 鸣叫次数
extern INT16U uwBuzzerCnt;

extern INT16U uwLedCnt;			// 
extern INT16U uwLedCmp;			// 
extern INT16U uwLedPeriod;		// 
extern INT16U uwLedTimes;		// 


// AD口宏定义
#define		ADC_CHANNEL_NUM		4

extern INT16U ADC_ConvertedValue[ADC_CHANNEL_NUM];

// Motor 宏定义
#define cStop					0	// 停止
#define cUp						1	// 上升/前倾
#define cDown					2	// 下降/后仰
#define cStopQuick				3	// 立即停止
#define cUpQuick				4	// 快速上升/前倾


#define cMotorPwmPeriod			(6400 - 1)					// 电机PWM周期 64M/6400 = 10K
#define cMotorStep				(cMotorPwmPeriod / 600)		// 运行步长


void sSuperTask(void);
void sGenEventTask(void);
void sSciTask(void);


void TimingDelay_Decrement(void);
void sInitialMCU(void);
extern void sGetCurrRole(void);
extern void sSaveRole(void);


#endif 

